package service

import (
	"im/global"
	"im/mapper"
	"im/model/request"
	"im/model/response"
	"im/utils"

	"github.com/gin-gonic/gin"
)

type UserContactService struct{}

// 添加好友
func (uca *UserContactService) AddContact(c *gin.Context) {
	var userContactReq request.UserContactReq
	if err := c.ShouldBind(&userContactReq); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("请求体解析失败", c)
		return
	}

	// Todo: 校验参数
	// 获取当前用户id
	if uid, err := utils.ParseTokenToUserId(c); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("Token解析错误", c)
		return
	} else {
		userContactReq.OwnerId = uid
	}

	if u, err := mapper.AddContact(userContactReq); err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Result(global.StatusOK, u, "操作成功", c)
	}
	c.Abort()
}

// 删除好友
func (uca *UserContactService) DelContact(c *gin.Context) {
	var userContactReq request.UserContactReq
	if err := c.ShouldBind(&userContactReq); err != nil {
		response.FailWithMessage("请求体解析失败", c)
		c.Abort()
	}

	// Todo: 校验参数
	// 获取当前用户id
	if uid, err := utils.ParseTokenToUserId(c); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("Token解析错误", c)
		c.Abort()
	} else {
		userContactReq.OwnerId = uid
	}
	if u, err := mapper.DelContact(userContactReq); err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Result(global.StatusOK, u, "操作成功", c)
	}
	c.Abort()
}

// 获取好友列表
func (uca *UserContactService) ListContacts(c *gin.Context) {
	var userContactReq request.UserContactReq
	// 获取当前用户id
	if uid, err := utils.ParseTokenToUserId(c); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("Token解析错误", c)
		c.Abort()
	} else {
		userContactReq.OwnerId = uid
	}

	// Todo: 校验参数
	if u, err := mapper.ListContacts(userContactReq); err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Result(global.StatusOK, u, "操作成功", c)
	}
	c.Abort()
}
